package com.yz.crm.employee.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yz.crm.db.model.BaseEmployee;
import com.yz.crm.employee.model.EmployeeSearchBean;
import com.yz.crm.employee.service.EmployeeService;
import com.yz.crm.passport.model.SessionUser;
import com.yz.crm.web.anno.JsonViewSupport;
import com.yz.util.WebUtils;

/**
 * 
 * */
@Controller
@RequestMapping("/admin/employee")
public class EmployeeAction {

	private static Log log = LogFactory.getLog(EmployeeAction.class);
	
	@Resource
	private EmployeeService employeeService;
	
	@RequestMapping("/index")
	public String index (HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		
		return "admin/employee/index";
	}
	
	@RequestMapping("/list")
	@JsonViewSupport(enableName="debug",modelName="result")
	public String list (String deptId,HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		
		SessionUser user = WebUtils.getLoginUser(request);
		
		EmployeeSearchBean bean = new EmployeeSearchBean();
		bean.setDeptId(deptId);
		bean.setCompanyId(user.getCompany().getId());
		bean.setCurrentPage(1);
		bean.setPageSize(6);
		
		
		modelMap.addAttribute("result", employeeService.listEmployee(bean));
		modelMap.addAttribute("deptId", deptId);
		modelMap.addAttribute("company", user.getCompany());
		
		return "admin/employee/list";
	}
	
	
	@RequestMapping("/add")
	public String add(String deptId,HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		
		modelMap.addAttribute("deptId", deptId);
		
		return "admin/employee/add";
	}
	
	@RequestMapping("/save")
	public String save (BaseEmployee employee,HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		
		SessionUser user = WebUtils.getLoginUser(request);
		
		if (employee.getId()!=null && !employee.getId().isEmpty()){
			BaseEmployee origin = employeeService.get(employee.getId());
			if (origin==null){
				log.info("id error");
				return "error";
			}else{
				origin.setName(employee.getName());
				origin.setJobTitle(employee.getJobTitle());
				origin.setPosition(employee.getPosition());
				origin.setOrderNum(employee.getOrderNum());
				origin.setModifierName(user.isAdmin()?user.getCompany().getName():user.getEmployee().getName());
				employee = origin;
			}
		}else{
			employee.setCompanyId(user.getCompany().getId());
			employee.setCreatorName(user.isAdmin()?user.getCompany().getName():user.getEmployee().getName());
		}
		
		employeeService.saveOrUpdate(employee);
		
		return "forward:/admin/employee/index";
	}
	
	
	@RequestMapping("/delete")
	public String delete (String id,HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		
		employeeService.delete(id);
		
		return "forward:/admin/employee/index";
	}
}
